//
//  HomeCommedCollectionView.m
//  Cat
//
//  Created by lushitong on 2018/12/19.
//  Copyright © 2018年 lushitong. All rights reserved.
//

#import "HomeCommedCollectionView.h"
#import "HttpHomeManager.h"

#import "HomeTopCell.h"
#import "HomeSortCell.h"
#import "ShopCollectionCell.h"
#import "HomeCollectionReusableView.h"
#import "RecomCollectionViewCell.h"
#import "HomeTodayCollectionViewCell.h"
#import "DetailViewController.h"
#import "HomeThreeCell.h"

#import "HomePriorityInfo.h"

static NSString *const cellID = @"FLHomeCollectionViewCell";

@interface HomeCommedCollectionView()<HomeSortDelegate>

@property (nonatomic, assign) BOOL isFirstOpen;     //首次打开
@property (nonatomic, strong) HomePriorityInfo *modelInfo;
@property (nonatomic, strong) NSMutableArray *groomArray;

@end

@implementation HomeCommedCollectionView

- (instancetype)initWithFrame:(CGRect)frame layout:(UICollectionViewFlowLayout *)layout {
	if (self = [super initWithFrame:frame layout:layout]) {
		self.addRefresh = YES;
		self.needLoadMore = YES;
		self.emheaderHeight = - NavigationBarHeight;
		self.collectionView.backgroundColor = [UIColor whiteColor];
		[self.collectionView mas_makeConstraints:^(MASConstraintMaker *make) {
			make.edges.equalTo(@0);
		}];
		self.collectionView.showsVerticalScrollIndicator = NO;
		self.collectionView.showsHorizontalScrollIndicator = NO;
		//注册cell
		[self.collectionView registerClass:[HomeTopCell class] forCellWithReuseIdentifier:NSStringFromClass([HomeTopCell class])];
		[self.collectionView registerClass:[HomeSortCell class] forCellWithReuseIdentifier:NSStringFromClass([HomeSortCell class])];
		[self.collectionView registerClass:[ShopCollectionCell class] forCellWithReuseIdentifier:NSStringFromClass([ShopCollectionCell class])];
		[self.collectionView registerClass:[RecomCollectionViewCell class] forCellWithReuseIdentifier:NSStringFromClass([RecomCollectionViewCell class])];
		[self.collectionView registerClass:[HomeTodayCollectionViewCell class] forCellWithReuseIdentifier:NSStringFromClass([HomeTodayCollectionViewCell class])];
		[self.collectionView registerClass:[HomeThreeCell class] forCellWithReuseIdentifier:NSStringFromClass([HomeThreeCell class])];
		
		[self.collectionView registerClass:[UICollectionViewCell class] forCellWithReuseIdentifier:cellID];
		
		//注册headerfooter
		[self.collectionView registerClass:[HomeCollectionReusableView class] forSupplementaryViewOfKind:UICollectionElementKindSectionHeader withReuseIdentifier:NSStringFromClass([HomeCollectionReusableView class])];
		self.isFirstOpen = YES;
		self.loadingBgColor = [UIColor whiteColor];
	}
	return self;
}

- (void)sendRequestWithCommed:(BOOL)needFreshLoading {
	
}

- (void)sendRequest {
	if (self.pageIndex == 1) {
		[self.groomArray removeAllObjects];
		[HttpHomeManager getHomeInfoWithSuccess:^(HomePriorityInfo *priortyModel) {
			self.modelInfo = priortyModel;
			[self.collectionView reloadData];
			[self finishLoadData:priortyModel.hots];
		} failure:^(NSInteger code, NSString * _Nonnull message) {
			
		}];
		[HttpHomeManager getIndexWithIndex:@(self.pageIndex) success:^(NSArray *groomList) {
			if (groomList.count == 0) {
				
			} else {
				[self.groomArray addObjectsFromArray:groomList];
			}
			dispatch_async(dispatch_get_main_queue(), ^{
				[self.collectionView reloadData];
			});
			[self finishLoadData:groomList];
		} failure:^(NSInteger code, NSString *message) {
			[FLProgressHUD showMessageCenter:message];
		}];
		
	} else {
		[HttpHomeManager getIndexWithIndex:@(self.pageIndex) success:^(NSArray *groomList) {
			if (groomList.count == 0) {
				
			} else {
				[self.groomArray addObjectsFromArray:groomList];
			}
			[self.collectionView reloadData];
			[self finishLoadData:groomList];
		} failure:^(NSInteger code, NSString *message) {
			[FLProgressHUD showMessageCenter:message];
		}];
	}
}

- (UIEdgeInsets)collectionView:(UICollectionView *)collectionView layout:(UICollectionViewLayout*)collectionViewLayout insetForSectionAtIndex:(NSInteger)section {
	return UIEdgeInsetsMake(0, kQKFitModuleDecimal(10.0), 0, kQKFitModuleDecimal(10.0));
}

- (CGSize)collectionView:(UICollectionView *)collectionView layout:(UICollectionViewLayout *)collectionViewLayout referenceSizeForHeaderInSection:(NSInteger)section {
	if (section == 3) {
		return CGSizeMake(ScreenWidth, kQKFitModuleDecimal(38));
	} else if (section == 4) {
		return CGSizeMake(ScreenWidth, kQKFitModuleDecimal(38 + 18));
	}
	return CGSizeZero;
}

- (UICollectionReusableView *)collectionView:(UICollectionView *)collectionView viewForSupplementaryElementOfKind:(NSString *)kind atIndexPath:(NSIndexPath *)indexPath {
	if ([kind isEqualToString:UICollectionElementKindSectionHeader]) {
		if (indexPath.section == 3) {
			HomeCollectionReusableView *view = [collectionView dequeueReusableSupplementaryViewOfKind:UICollectionElementKindSectionHeader withReuseIdentifier:NSStringFromClass([HomeCollectionReusableView class]) forIndexPath:indexPath];
			view.leftTitle = @"今日热销";
			return view;
		} else if (indexPath.section == 4) {
			HomeCollectionReusableView *view = [collectionView dequeueReusableSupplementaryViewOfKind:UICollectionElementKindSectionHeader withReuseIdentifier:NSStringFromClass([HomeCollectionReusableView class]) forIndexPath:indexPath];
			view.leftTitle = @"精品推荐";
			return view;
		}
	}
	return [UICollectionReusableView new];
}

- (UICollectionViewCell *)collectionView:(UICollectionView *)collectionView cellForItemAtIndexPath:(NSIndexPath *)indexPath {
	if (indexPath.section == 0) {
		HomeTopCell *bannerCell = [collectionView dequeueReusableCellWithReuseIdentifier:NSStringFromClass([HomeTopCell class]) forIndexPath:indexPath];
		[bannerCell setBannerArray:self.modelInfo.banners];
		return bannerCell;
	} else if (indexPath.section == 1) {
		HomeSortCell *cell =  [collectionView dequeueReusableCellWithReuseIdentifier:NSStringFromClass([HomeSortCell class]) forIndexPath:indexPath];
		[cell setSortArray:self.modelInfo.indexCategorylist];
		return cell;
	} else if (indexPath.section == 2) {
		HomeThreeCell *cell = [collectionView dequeueReusableCellWithReuseIdentifier:NSStringFromClass([HomeThreeCell class]) forIndexPath:indexPath];
		[cell setRecArray:self.modelInfo.recommends];
		return cell;
	} else if (indexPath.section == 3) {
		HomeTodayCollectionViewCell *cell = [collectionView dequeueReusableCellWithReuseIdentifier:NSStringFromClass([HomeTodayCollectionViewCell class]) forIndexPath:indexPath];
		
		[cell setHotsArray:self.modelInfo.hots];
		return cell;
	} else if (indexPath.section == 4) {
		ShopCollectionCell *cell = [collectionView dequeueReusableCellWithReuseIdentifier:NSStringFromClass([ShopCollectionCell class]) forIndexPath:indexPath];
		[cell setModel:[self.groomArray safeObjectAtIndex:indexPath.row]];
		return cell;
	}
	UICollectionViewCell *cell = [collectionView dequeueReusableCellWithReuseIdentifier:cellID forIndexPath:indexPath];
	return cell;
}


- (void)collectionView:(UICollectionView *)collectionView didSelectItemAtIndexPath:(NSIndexPath *)indexPath {
	[collectionView deselectItemAtIndexPath:indexPath animated:YES];
	if (indexPath.section == 4) {
		GroomsModel *mode = [self.groomArray safeObjectAtIndex:indexPath.row];
		DetailViewController *de = [[DetailViewController alloc] init];
		de.proudctID = mode.productId;
		[self.currentNavigationController pushViewController:de animated:YES];
	}
}

- (NSInteger)numberOfSectionsInCollectionView:(UICollectionView *)collectionView {
	return 5;
}

- (NSInteger)collectionView:(UICollectionView *)collectionView numberOfItemsInSection:(NSInteger)section {
	if (section == 4) {
		return self.groomArray.count;
	}
	return 1;
}

- (CGSize)collectionView:(UICollectionView *)collectionView layout:(UICollectionViewLayout*)collectionViewLayout sizeForItemAtIndexPath:(NSIndexPath *)indexPath {
	if (indexPath.section == 0) {
		return CGSizeMake(ScreenWidth, kQKFitModuleDecimal(150.0));
	} else if (indexPath.section == 1) {
		return CGSizeMake(ScreenWidth, kQKFitModuleDecimal(90.0));
	} else if (indexPath.section == 2) {
		return CGSizeMake(ScreenWidth, kQKFitModuleDecimal(200 + 24.0 + 8.0));
	} else if (indexPath.section == 3) {
		return CGSizeMake(ScreenWidth, kQKFitModuleDecimal(13 + 104 + 60 + 13));
	} else if (indexPath.section == 4) {
		CGFloat width = (ScreenWidth - kQKFitModuleDecimal(24 + 9)) / 2;
		return CGSizeMake(width,width + kQKFitModuleDecimal(94));
	}
	return CGSizeMake(0.0, 0.0);
}


- (NSMutableArray *)groomArray {
	if (!_groomArray) {
		_groomArray = [[NSMutableArray alloc] init];
	}
	return _groomArray;
}
@end
